<template>
    <div>
        <img :src="dataimg" alt="" width="90px" height="36px"> <br>
        验证码： <input type="text" v-model="verifyCode" @keyup.enter="goLogin"> <br>
        <button @click="goLogin">获取密钥加密并登录</button>
    </div>
</template>

<script>
import { getPublicKey,login,getCode,getMeunList } from '../../api/login'
import { encrypt } from '../../utils/encrypt'
import { mapMutations } from 'vuex'
export default {
    name:'login',
    data() {
        return {
            randomKey: '',
            dataimg: '',
            verifyCode: ''
        };
    },
    methods:{
        ...mapMutations('user',['EditName']), //将模块 user里面的方法绑定到啦this上 vue实例上
        async goLogin(){
            let res = await getPublicKey()
            let password =  encrypt(res.data,'Zt@@2021') //加密密码
            let loginres = await login({
                loginName:'admin',
                password,
                publicKey: res.data,
                type: 1,
                verifyCode: this.verifyCode,
                randomKey: this.randomKey,
            })
            console.log('用户信息',loginres)
            // this.EditName('wyn')
            this.$store.commit('user/EdituserInfo',loginres.data)
            // this.$store.dispatch('user/acEditName','wyn')
            // let menures = await getMeunList({
            //     type: 1
            // })
            // console.log('菜单信息',menures)
            this.$router.push({
                path:'/home'
            })
        },
        // 获取验证码
        getCodes(){
            getCode().then(res => {
                this.randomKey = res.data.randomKey
                this.dataimg = "data:image/jpg;base64,"+res.data.img
            })
        },
       
    },
    mounted(){
        this.getCodes()
    }
};
</script>

<style scoped lang="scss">

</style>
